<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/9/12 15:27
 * Desc: 处理响应数据
 */
trait Trait_Api_Response
{
    /**
     * 过滤数据->只保留指定字段
     * @param array|string $filter_keys 需要保留的字段
     * @param mixed|array $data 需要过滤的数据
     * @return array
     */
    public function filter_data($filter_keys, $data)
    {
        $filter_keys = is_string($filter_keys) ? explode(',', $filter_keys) : $filter_keys;
        if(!empty($data) && is_array($data))
        {
            $result = array_filter($data, function ($key) use ($filter_keys) {
                if(in_array($key, $filter_keys)){
                    return true;
                }
                return false;
            },ARRAY_FILTER_USE_KEY);
        }else{
            $result = $data;
        }
        return $result;
    }
}